jQuery(document).ready(function($) {
    var mouth = 6; //三个月
    var d = new Date();
    var pid = $('#time');
    var sumType = 0;
    var chosetime = [];
    var body = $('body');
    var year = d.getFullYear();
    var baginTime,endTime;
    for (var i = 0; i < mouth; i++) {
        pid.append('<div class="year year-'+i+'">'+(year+i)+'</div>');
        var tempYearName = 'mouth-'+(year+i);
        var tempYearStr = '<div year="'+(year+i)+'" id="'+tempYearName+'" class="mouth mouth-'+i+'"></div>';
        pid.append(tempYearStr);
    }
    var mid = $('.mouth');
    for (var i = 0; i < mid.length; i++) {
        var temp = '';
        for (var j = 0; j < 12 ; j++) {
            var tempMouthName = mid.eq(i).attr('year')+'-'+(j+1);
            var timestamp = +new Date(tempMouthName);
            temp += '<div ontime="'+timestamp+'" class="fix '+tempMouthName+'"><span class="nextMouth">'+(j+1)+'月</span></div>';
        }
        mid.eq(i).append(temp);
    }
    body.on('click','.fix',function () {
        sumType++;
        time = $(this).attr('ontime');
        choseTimeData(time,$(this));
    });
    choseTimeData = function (time,that) {
        if((sumType%2) == 1){
            baginTime = time;
        }else{
            endTime = time;
        }
        addStartName(sumType,that);
    };
    addStartName = function (sumType,that) {
        var clength = body.find('.c_chose').length;
        if(body.find('.c_start').length == 0){
            $('.c_start').remove();
            that.append('<span class="c_start c_chose c_num_'+sumType+'">开始时间</span>');
        }else if(body.find('.c_end').length == 0){
            $('.c_end').remove();
            that.append('<span class="c_end c_chose c_num_'+sumType+'">结束时间</span>');
        }else{
            $('.c_start').remove();
            that.append('<span class="c_start c_chose c_num_'+sumType+'">开始时间</span>');
            delDomSpan(sumType);
        }
        //如果结束时间小于开始时间则,把结束时间变为开始时间,开始时间变成结束时间
        if(endTime < baginTime){
            $('.c_start').html('结束时间');
            $('.c_end').html('开始时间');
            body.find('.c_end').removeClass('c_end').addClass('c_start_bak');
            body.find('.c_start').removeClass('c_start').addClass('c_end_bak');

            body.find('.c_end_bak').removeClass('c_end').addClass('c_end');
            body.find('.c_start_bak').removeClass('c_start_bak').addClass('c_start');
            delDomSpan(sumType);
        }
        console.log('开始时间->'+baginTime);
        console.log('结束时间->'+endTime);
    };
    delDomSpan = function (sumType) {
        for (var i = 0; i < sumType-1; i++) {
            $('.c_num_'+i).remove();
        }
    }
});